home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWFiles / Include / FWAccBuf.h next >
Encoding:
Text File  |  1995-11-08  |  6.8 KB  |  202 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:        FWAccBuf.h
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef FWACCBUF_H
  11. #define FWACCBUF_H
  12.  
  13. #ifndef FWEXCDEF_H
  14. #include "FWExcDef.h"
  15. #endif
  16.  
  17. #ifndef FWAUTODE_H
  18. #include "FWAutoDe.h"
  19. #endif
  20.  
  21. #if FW_LIB_EXPORT_PRAGMAS
  22. #pragma lib_export on
  23. #endif
  24.  
  25. //========================================================================================
  26. //    CLASS FW_CPrivFileAccessBuffer
  27. //
  28. //  The FW_CPrivFileAccessBuffer is intended to be used with the FW_CFileSink class to
  29. //    provide buffered output of the data.  This class is an implementation class and
  30. //    should not be used as a stand-alone class.
  31. //
  32. //  The way the buffer works, is that it maintains a window over the file stream.  That
  33. //    window is the RAM-based buffer which mirrors the data on the disk.  Buffer methods 
  34. //    that read and write data, do so within that window.  When the currentPosition moves
  35. //    beyond the window's bounds, ReadPeeks and WritePeeks will return zero bytes read.  
  36. //    When FW_CFileSink detects this it will re-initialize the data in the buffer to
  37. //    the new data window based on the currentPosition.
  38. //
  39. //  ReadPeek and WritePeek do not actually read or write the data to the disk.  The 
  40. //    FW_CFileSink is responsible for getting a pointer to the buffer and filling it
  41. //    with data.  The AccessBuffer is just responsible for caching the information and
  42. //    maintaining the pointers to the data.
  43. //========================================================================================
  44.  
  45. class FW_CLASS_ATTR FW_CPrivFileAccessBuffer FW_AUTO_DESTRUCT_OBJECT
  46. {
  47. public:
  48.  
  49.     enum
  50.     {
  51.         kInvalid, kReadPeek, kWritePeek
  52.     };
  53.  
  54.  
  55.     FW_CPrivFileAccessBuffer(long capacity);
  56.         // Creates an instance with the specified capacity.  The memory
  57.         //   for the buffer is not allocated until Initialize is called.
  58.         // The default buffer type is kInvalid.
  59.     
  60.     FW_CPrivFileAccessBuffer(const FW_CPrivFileAccessBuffer& otherBuffer);
  61.         // Copy constructor.  Initializes this buffer to be the same size
  62.         //   as otherBuffer.  It does not copy the data from otherBuffer.
  63.     
  64.     ~ FW_CPrivFileAccessBuffer();
  65.     
  66.     void Initialize(int type,
  67.                     long filePosition,
  68.                     long validBytes);
  69.         // Allocates memory for the buffer and initializes the type and validity
  70.         //   of the buffer.
  71.                 
  72.     FW_CPrivFileAccessBuffer& operator=(const FW_CPrivFileAccessBuffer& otherBuffer);
  73.         // Assignment operator.  Changes the size of this buffer only.  Does not
  74.         //   copy the data from the other buffer.
  75.         
  76.                     
  77.     //===========================================================
  78.     // Read/Write
  79.     //===========================================================
  80.                     
  81.     void Invalidate();
  82.         // Re-initializes the buffer to contain invalid data.  The buffer must
  83.         //   be re-initialized before the next ReadPeek or WritePeek.
  84.     
  85.     void* ReadPeek(long currentPosition,
  86.                    long& availableReadBytes);
  87.         // Returns a pointer to the data in the buffer starting at currentPosition.
  88.         //   availableReadBytes returns the number of valid bytes in the buffer
  89.         //   returned.
  90.                    
  91.     void ReadPeekAdvance(long currentPosition,
  92.                          long bytesRead);
  93.         // Advances the ReadPeek buffer to indicate that the user has read the
  94.         //   number of bytes specified by bytesRead.  
  95.         // This routine should be called after the user uses the data from the buffer.
  96.         //   Otherwise, the same bytes will be returned by the next ReadPeek.
  97.                          
  98.     void* WritePeek(long currentPosition,
  99.                     long& availableWriteBytes);
  100.         // Returns a pointer to the buffer starting at currentPosition.  The
  101.         //   number of bytes available for writing is returned in availableWriteBytes.
  102.                     
  103.     void WritePeekAdvance(long currentPosition,
  104.                           long bytesWritten);
  105.         // Advances the WritePeek buffer to indicate that the user has written the
  106.         //   number of bytes specified by bytesWritten starting at currentPosition.
  107.         // This routine should be alled after the user writes the data.  Otherwise,
  108.         //   the data could be overwritten by future writes.
  109.  
  110.  
  111.     //===========================================================
  112.     // Accessor methods
  113.     //===========================================================
  114.  
  115.     long GetCapacity() const;
  116.         // Returns the capacity of the data buffer.
  117.     
  118.     void* GetAddress() const;
  119.         // Returns a pointer to the entire buffer.
  120.     
  121.     long GetInitialPosition() const;
  122.         // Returns the position in the file where the buffer starts.
  123.     
  124.     long GetBytesWritten() const;
  125.         // Returns the number of bytes that have been written into the buffer.
  126.         //   Used only for Write buffers.
  127.     
  128.     FW_Boolean IsDirty() const;
  129.         // Used for Write buffers only.  Returns TRUE if there is data in the buffer
  130.         //   that needs to be written.
  131.  
  132. private:
  133.  
  134.     int fType;
  135.     char* fBuffer;
  136.     long fCapacity;
  137.     long fInitialPosition;
  138.     long fValidBytes;
  139.     long fBytesWritten;
  140. };
  141.  
  142.  
  143. //----------------------------------------------------------------------------------------
  144. // FW_CPrivFileAccessBuffer::Invalidate
  145. //----------------------------------------------------------------------------------------
  146.  
  147. inline void FW_CPrivFileAccessBuffer::Invalidate()
  148. {
  149.     Initialize(kInvalid, 0, 0);
  150. }
  151.  
  152. //----------------------------------------------------------------------------------------
  153. // FW_CPrivFileAccessBuffer::GetCapacity
  154. //----------------------------------------------------------------------------------------
  155.  
  156. inline long FW_CPrivFileAccessBuffer::GetCapacity() const
  157. {
  158.     return fCapacity;
  159. }
  160.  
  161. //----------------------------------------------------------------------------------------
  162. // FW_CPrivFileAccessBuffer::GetAddress
  163. //----------------------------------------------------------------------------------------
  164.  
  165. inline void* FW_CPrivFileAccessBuffer::GetAddress() const
  166. {
  167.     return fBuffer;
  168. }
  169.  
  170. //----------------------------------------------------------------------------------------
  171. // FW_CPrivFileAccessBuffer::GetInitialPosition
  172. //----------------------------------------------------------------------------------------
  173.  
  174. inline long FW_CPrivFileAccessBuffer::GetInitialPosition() const
  175. {
  176.     return fInitialPosition;
  177. }
  178.  
  179. //----------------------------------------------------------------------------------------
  180. // FW_CPrivFileAccessBuffer::GetBytesWritten
  181. //----------------------------------------------------------------------------------------
  182.  
  183. inline long FW_CPrivFileAccessBuffer::GetBytesWritten() const
  184. {
  185.     return fBytesWritten;
  186. }
  187.  
  188. //----------------------------------------------------------------------------------------
  189. // FW_CPrivFileAccessBuffer::IsDirty
  190. //----------------------------------------------------------------------------------------
  191.  
  192. inline FW_Boolean FW_CPrivFileAccessBuffer::IsDirty() const
  193. {
  194.     return (fType == kWritePeek) && (fBytesWritten != 0);
  195. }
  196.  
  197. #if FW_LIB_EXPORT_PRAGMAS
  198. #pragma lib_export off
  199. #endif
  200.  
  201. #endif
  202.